package com.chase.framerwork.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

/**
 * Bean工具类,bean转换成Map,JSON等
 * 
 * @Author Chase
 * @Date 2013-10-22
 */
public class BeanUtil
{
	/**
	 * set
	 */
	private static final String SET = "set";

	/**
	 * get
	 */
	private static final String GET = "get";

	/**
	 * 转换属性名成set方法
	 * 
	 * @param propertyName
	 * @return
	 */
	public static String convertorProperty2SetMethod(String propertyName)
	{
		if (StringUtil.isEmpty(propertyName))
		{
			return null;
		}

		Character firstChar = propertyName.toCharArray()[0];
		String upstr = Character.toString(firstChar).toUpperCase();

		return StringUtil.appendAll(SET, upstr, propertyName.substring(1));
	}

	/**
	 * set方法名称转换成属性名称
	 * 
	 * @param methodName
	 * @return
	 */
	public static String convertorSetMethod2Property(String methodName)
	{
		if (StringUtil.isEmpty(methodName) || !methodName.startsWith(SET))
		{
			return null;
		}

		String str = methodName.substring(3, methodName.length());
		Character firstChar = str.toCharArray()[0];
		String endStr = methodName.substring(4, methodName.length());

		return Character.toString(firstChar).toLowerCase() + endStr;
	}

	/**
	 * 转换属性名成get方法
	 * 
	 * @param propertyName
	 * @return
	 */
	public static String convertorProperty2GetMethod(String propertyName)
	{
		if (StringUtil.isEmpty(propertyName))
		{
			return null;
		}

		Character firstChar = propertyName.toCharArray()[0];
		String upstr = Character.toString(firstChar).toUpperCase();

		return StringUtil.appendAll(GET, upstr, propertyName.substring(1));
	}

	/**
	 * get方法名转换成属性名
	 * 
	 * @param methodName
	 * @return
	 */
	public static String convertorGetMethod2Property(String methodName)
	{
		if (StringUtil.isEmpty(methodName) || !methodName.startsWith(GET))
		{
			return null;
		}

		String str = methodName.substring(3, methodName.length());
		Character firstChar = str.toCharArray()[0];
		String endStr = methodName.substring(4, methodName.length());

		return Character.toString(firstChar).toLowerCase() + endStr;
	}

	/**
	 * Bean转换成Map
	 * 
	 * @param t
	 * @return
	 */
	public static <T> Map<String, Object> bean2Map(T t)
	{

		Map<String, Object> map = new HashMap<>();

		Class<?> clazz = t.getClass();
		Method[] methods = clazz.getMethods();
		for (Method method : methods)
		{
			if (method.getName().startsWith(GET))
			{
				String propertyName = convertorGetMethod2Property(method.getName());
				Object value = ReflectionUtil.invokeGetterMethod(t, propertyName);

				map.put(propertyName, value);
			}
		}

		return map;
	}

	/**
	 * Bean转换成JSON对象
	 * 
	 * @param t
	 * @return
	 */
	public static <T> JSONObject bean2JSON(T t)
	{
		JSONObject jsonObject = JSONObject.fromObject(t);

		return jsonObject;
	}

	/**
	 * Bean转换成JSON格式字符串
	 * 
	 * @param t
	 * @return
	 */
	public static <T> String bean2JSONData(T t)
	{
		return bean2JSON(t).toString();
	}
}
